﻿<?php session_start(); 
if($_SESSION['ID'] != null){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/index.css" rel="stylesheet" type="text/css" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />

<title>File Receive</title>


<?php
   $filename = $_FILES['file']['name'];
   $filename_tmp = $_FILES['file']['tmp_name'];
   $filename_dst = "tmp/$filename";
   move_uploaded_file ($filename_tmp, $filename_dst);
   $pname=$_POST['titletxt'];
   $describe=$_POST['contxt'];

?>

<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<p class="style1">&nbsp;</p>
<p>專案建立成功!</p>
<p>專案名稱：<?php echo $pname; ?></p>
<p>專案內容介紹：<?php echo $describe; ?></p>

<p>
  <?php
// 含入系統（資料庫）設定檔
require('config.inc.php');
require('db.php');

$UserNo=$_SESSION['UserNo'];


// 連結資料庫伺服器
db_connect();

// 選擇資料庫
db_select();
?>

  
<?php 
//將nmea檔轉為gpx檔
$input_nmea=fopen($filename_dst,"r");
$outputname=date("Y-m-d-h-i-s");
$fp = fopen("output/".$outputname, 'w');

fwrite($fp, "<gpx>" );
if(!$input_nmea){
   echo "false";
}else{
   $i=0;
   $line=fgets($input_nmea,100);
 
while(!feof($input_nmea)){
		$line=fgets($input_nmea,100);
			$i++;
		if(substr($line,0,6)=='$GPRMC'){
		  //時間
		  $time = substr ($line,7,10);
		  //緯度 
		  $lat_dir=$line[30];
          $lat = substr ($line,20,9); 
		  $lat_num=(float)$lat;
		  $lat_num=$lat_num/100;
		  $lat_int=round($lat_num);
		  $lat_num=$lat_num-$lat_int;
		  $lat_num=$lat_num*100/60+$lat_int;
		  if($lat_dir=='S'){
		     $lat_num=-1*$lat_num;		  
		  }
		  //經度
		  $lon_dir=$line[43];
          $lon = substr ($line,32,10);
		  $lon_num=(float)$lon;
		  $lon_num=$lon_num/100;
		  $lon_int=round($lon_num);
		  $lon_num=$lon_num-$lon_int;
		  $lon_num=$lon_num*100/60+$lon_int;
		  if($lon_dir=='W'){
		     $lon_num=-1*$lon_num;		  
		  }
		  //日期
		  $num=0;
		  $j=0;
		  while($line){
		     if($line[$j]==','){
			     $num++;
			 }
			 if($num==9){
			 $date= substr ($line,$j+1,6) ;
			 break;
			 }
			 $j++;
		  }
$all="<trkpt lat=\"".substr($lat_num,0,12)."\" lon=\"".substr($lon_num,0,13)."\">\n<time>"."20".       $date[4].$date[5]."-".$date[2].$date[3]."-".$date[0].$date[1].
	"T".$time[0].$time[1].":".$time[2].$time[3].":".$time[4].$time[5]."Z"."</time>\n  </trkpt>\n";
          
		  fwrite($fp, $all );
        }
   }
    //fclose($filename_dst);
}
fwrite($fp, "</gpx>" );
fclose($fp);
?>
  
<?php
// 將專案資訊存入資料庫

$sql = 'insert into project(pname,fname,descri,UserNo) values';
$sql .= '(\'' . $pname . '\',\'' . $outputname . '\',\'' . $describe . '\',\'' . $UserNo . '\') ;';
$result = mysql_query($sql) or die('Invalid query: ' . mysql_error());

// 顯示新增帳號
echo "檔案名稱新增成功<br>";

// 資料庫連結斷線
db_close();


?>
</p>
<p>&nbsp;</p>
<p>  <a target='blank' href = 'proj_edit.html?No=<?php echo $outputname;?>'>編輯軌跡專案</a></p>
</body>
</html>

<?php
}else{
     header("Location:"."login.html");
}
?>